import request from "@/utils/request";
import axios from "axios";
import { getToken } from "xqjr-module-auth";
import { notification } from "ant-design-vue";
export function findByDefault(params) {
  return request({
    url: "/admin/api/v1/customTemplateFields/findByDefault",
    method: "GET",
    params
  });
}

export function findBySite(params) {
  return request({
    url: "/admin/api/v1/customTemplateFields/findBySite",
    method: "GET",
    params
  });
}

export function generateQRCode(data) {
  return request({
    url: "/recruit2/api/v1/jobSeekers/generateQRCode",
    method: "post",
    data
  });
}

export function editCustomTemplate(data) {
  return request({
    url: "/admin/api/v1/customTemplates/editCustomTemplate",
    method: "put",
    data
  });
}

export function onPostDownload(url, name) {
  axios({
    url: "/file/api/v1/site/download",
    method: "post",
    data: {
      siteFileUrls: [url]
    },
    responseType: "blob",
    headers: {
      token: getToken()
    }
  })
    .then((res) => {
      f(res, name);
    })
    .catch((err) => {
      console.log("error log --> ", err);
    });
}

const f = (res, name) => {
  if (res.data) {
    const rspHeaders = res.headers;
    const blob = new Blob([res.data], { type: "application/octet-stream" });
    const link = document.createElement("a");
    link.href = URL.createObjectURL(blob);
    document.body.appendChild(link);
    if (rspHeaders["content-disposition"]) {
      try {
        const fileName = decodeURI(rspHeaders["content-disposition"].split(";")[1].split("=")[1].replaceAll('"', ""));
        const fileNameArr = fileName.split(".");
        link.download = name + "." + fileNameArr[1];
        link.click();
        URL.revokeObjectURL(link.href);
        document.body.removeChild(link);
      } catch (err) {
        console.log("error log --> ", err);
      }
    } else {
      notification.error({ message: "下载失败" });
    }
  }
};
